﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Data.NHibernate.Generic.Support;
using Dukous.Domain;
using NHibernate;

namespace Dukous.Dao.NHibernate
{
    /// <summary>
    /// 实体 DAO
    /// </summary>
    /// <typeparam name="T">实体类型</typeparam>
    /// <typeparam name="IdT">ID 类型</typeparam>
    public abstract class EntityDao<T, IdT> : HibernateDaoSupport, IEntityDao<T, IdT>
        where T : IEntity<IdT>
    {
        #region IEntityDao<T,IdT> 成员

        /// <summary>
        /// 根据 ID 查找实体。找不到则返回 null
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public T FindById(IdT id)
        {
            return this.HibernateTemplate.Get<T>(id);
        }

        /// <summary>
        /// 查询所有实体
        /// </summary>
        /// <returns></returns>
        public IList<T> SelectAll()
        {
            return this.HibernateTemplate.LoadAll<T>();
        }

        /// <summary>
        /// 保存实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns>ID</returns>
        public IdT Save(T entity)
        {
            return (IdT)this.HibernateTemplate.Save(entity);
        }

        /// <summary>
        /// 更新指定的实体
        /// </summary>
        /// <param name="entity"></param>
        public void Update(T entity)
        {
            this.HibernateTemplate.Update(entity);
        }

        /// <summary>
        /// 保存或更新实体
        /// </summary>
        /// <param name="entity"></param>
        public void SaveOrUpdate(T entity)
        {
            this.HibernateTemplate.SaveOrUpdate(entity);
        }

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="entity"></param>
        public void Delete(T entity)
        {
            this.HibernateTemplate.Delete(entity);
        }

        /// <summary>
        /// 刷新实体（即从数据库中重新获取相应值）
        /// </summary>
        /// <param name="entity"></param>
        public void Refresh(T entity)
        {
            this.HibernateTemplate.Refresh(entity);
        }

        /// <summary>
        /// 将实体从会话缓存中移除
        /// </summary>
        /// <param name="entity"></param>
        public void Evict(T entity)
        {
            this.HibernateTemplate.Evict(entity);
        }

        /// <summary>
        /// 将实体与会话重新关联
        /// </summary>
        /// <param name="entity"></param>
        public void Lock(T entity)
        {
            this.HibernateTemplate.Lock(entity, LockMode.Read);
        }

        /// <summary>
        /// 将当前挂起的各项操作（save、update、delete）刷新到数据库中
        /// </summary>
        public void Flush()
        {
            this.HibernateTemplate.Flush();
        }

        /// <summary>
        /// 清除当前会话缓存中的所有数据，取消挂起的各项操作（save、update、delete）
        /// </summary>
        public void Clear()
        {
            this.HibernateTemplate.Clear();
        }

        #endregion
    }
}
